using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using System.Globalization;
using System.Net.NetworkInformation;
namespace LyonMag
{

    public partial class Detail : PhoneApplicationPage
    {
        App app = App.Current as App;
        string title = string.Empty;
        string header = string.Empty;
        string imageurl = string.Empty;
        string category = string.Empty;
        string date = string.Empty;
        string commentscount = string.Empty;
        string id = string.Empty;
        string thumbtitle = string.Empty;
        public Detail()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            try
            {
            
             
                NavigationContext.QueryString.TryGetValue("title", out title).ToString();
                NavigationContext.QueryString.TryGetValue("header", out header).ToString();
                NavigationContext.QueryString.TryGetValue("imageurl", out imageurl).ToString();
                NavigationContext.QueryString.TryGetValue("category", out category).ToString();
                NavigationContext.QueryString.TryGetValue("date", out date).ToString();
                NavigationContext.QueryString.TryGetValue("commentscount", out commentscount).ToString();
                NavigationContext.QueryString.TryGetValue("id", out id).ToString();
                if (id == null)
                {
                    for (int i = 0; i < app.data.Count(); i++)
                    {
                        if (app.data[i].Title == title)
                        {
                            id = app.data[i].Id;
                        }
                    }
                }
                if (imageurl == null)
                {
                    for (int i = 0; i < app.data.Count(); i++)
                    {
                        if (app.data[i].Title == title)
                        {
                            imageurl = app.data[i].AppImage;
                        }
                    }
                }
                 
                NavigationContext.QueryString.TryGetValue("thumb", out thumbtitle).ToString();
                    if(thumbtitle==null)
                    {
                          for (int i = 0; i < app.data.Count(); i++)
                    {
                        if (app.data[i].Title == title)
                        {
                            thumbtitle = app.data[i].Thumbtitle;
                        }
                    }
                    }
                System.Windows.Media.Imaging.BitmapImage nit = new System.Windows.Media.Imaging.BitmapImage();
                nit.UriSource = new Uri(imageurl);
                img.Source = nit;
                txtTile.Text = category;
                DateTime MyDateTime;
                MyDateTime = new DateTime();
                MyDateTime = Convert.ToDateTime(date);
                string inputFormat = "dd-MM-yyyy HH:mm:ss";
                txtDate.Text = MyDateTime.ToString(inputFormat);
             //   txtDescription.Text = header;
                txtHeader.Text = title;
                txtccount.Text ="(" + commentscount +")";
                txtThumbTitle.Text = thumbtitle;
                for (int i = 0; i < app.data.Count(); i++)
                {
                    if (app.data[i].Id == id)
                    {
                        txtDescription.Text = app.data[i].Fulltext;
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();

                if (NetworkInterface.GetIsNetworkAvailable() == true)
                {
                    // MessageBox.Show(NetworkInterface.GetIsNetworkAvailable().ToString());
                }
                else
                {
                    MessageBox.Show("Internet Not Availabe", "LyonMag", MessageBoxButton.OK);
                }
            }
        }

        private void stkTwitter_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                ShareLinkTask obj_share = new ShareLinkTask();
                obj_share.Message = title;
                obj_share.Title = title;
                obj_share.LinkUri = new Uri("http://www.lyonmag.com");
                obj_share.Show();
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }

        private void stkEmail_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                ShareLinkTask obj_share = new ShareLinkTask();
                obj_share.Message = title;
                obj_share.Title = title;
                obj_share.LinkUri = new Uri("http://www.lyonmag.com");
                obj_share.Show();
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }

        private void stkFacebook_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                ShareLinkTask obj_share = new ShareLinkTask();
                obj_share.Message = title;
                obj_share.Title = title;
                obj_share.LinkUri = new Uri("http://www.lyonmag.com");
                obj_share.Show();
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }

        private void stkComments_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri("/comments.xaml?id=" + id, UriKind.RelativeOrAbsolute));
        }
    }
}